package mis.ds.ps.ui.swing;

//import javax.swing.UIManager;
//import javax.swing.UnsupportedLookAndFeelException;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import javax.swing.*;

public class UIStart extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;
	private final int	ITEM_PLAIN	=	0;	// Item types
	private final int	ITEM_CHECK	=	1;
	private final int	ITEM_RADIO	=	2;
	
	private JDesktopPane desktop;
	
	private JMenuBar menubar;
	private JMenu menuFile;	
	private JMenuItem menuFileCarga;
	private JMenuItem menuFilePreferencias;
	private JMenuItem menuFileExit;
	private JMenu menuHelp;
	private JMenuItem menuHelpAbout;
	
	private ImportComissao importComissao;
	private ImportPreferencias importPreferencias;
	private static Logger logger = Logger.getLogger(UIStart.class);
	
	public UIStart() {
		super("TimeTable Application");
//		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
//        setBounds(0,0,screenSize.width, screenSize.height);
		
		//Make the big window be indented 50 pixels from each edge
        //of the screen.
        int inset = 50;
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds(inset, inset,
                  screenSize.width  - inset*2,
                  screenSize.height - inset*2);
        
      //Set up the GUI.
        desktop = new JDesktopPane(); //a specialized layered pane
        setContentPane(desktop);

        //Make dragging a little faster but perhaps uglier.
        desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
        
        addMenu();

	}
	
	private void addMenu() {
		// Create the menu bar
		menubar = new JMenuBar();
		
		// Set this instance as the application's menu bar
		setJMenuBar(menubar);
		
		// Build the File sub-menu
		menuFile = new JMenu("File");
		menuFile.setMnemonic('F');
		menubar.add(menuFile);
		
		// Create File items
//		menuFileCarga = new JMenuItem("Carga");
//		menuFileCarga.addActionListener(this);
//		menuFile.add(menuFileCarga);
		
		menuFileCarga = CreateMenuItem(menuFile, ITEM_PLAIN, 
				"Comissão Científica...", null, 'C', null);
		
		menuFilePreferencias = CreateMenuItem(menuFile, ITEM_PLAIN, 
				"Preferências Docentes...", null, 'P', null);
		
		menuFile.addSeparator();
		
//		menuFileExit = new JMenuItem("Exit");
//		menuFile.add(menuFileExit);				
//		menuFileExit.addActionListener(this);
		
		menuFileExit = CreateMenuItem( menuFile, ITEM_PLAIN,
				"Exit", null, 'x',
				"Exit Application" );
		
		// Build the File sub-menu
		menuHelp = new JMenu("Help");
		menuHelp.setMnemonic('H');
		menubar.add(menuHelp);
		
		menuHelpAbout = CreateMenuItem(menuHelp, ITEM_PLAIN, 
				"About...", null, 'A', "Aplication credits....");
				
	}
	
	public JMenuItem CreateMenuItem(JMenu menu, int iType, String sText,
			ImageIcon image, int acceleratorKey, String sToolTip) {
		
		// Create the item
		JMenuItem menuItem;
		
		switch(iType) {
			case ITEM_RADIO:
				menuItem = new JRadioButtonMenuItem();
				break;
			
			case ITEM_CHECK:
				menuItem = new JCheckBoxMenuItem();
				break;
			
			default:
				menuItem = new JMenuItem();
				break;
		}
		
		// Add the item test
		menuItem.setText(sText);
		
		// Add the optional icon
		if(image != null)
		menuItem.setIcon(image);
		
		// Add the accelerator key
		if(acceleratorKey > 0)
		menuItem.setMnemonic(acceleratorKey);
		
		// Add the optional tool tip text
		if(sToolTip != null)
		menuItem.setToolTipText(sToolTip);
		
		// Add an action handler to this menu item
		menuItem.addActionListener(this);
		
		menu.add(menuItem);
		
		return menuItem;
	}
	
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		System.out.println(e.getActionCommand());
		if (e.getActionCommand() == "Comissão Científica...") {
			importComissao = new ImportComissao();			
			importComissao.setVisible(true);
	        desktop.add(importComissao);
		} else if (e.getActionCommand() == "Preferências Docentes...") {
			importPreferencias = new ImportPreferencias();
			importPreferencias.setVisible(true);
			desktop.add(importPreferencias);
			
		} else if (e.getActionCommand() == "Exit") {
			System.exit(0);
		}
		
	}		

	
	private static void createMainGUI() {
		UIStart that = new UIStart();
		that.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		that.setVisible(true);
	}
	
	public static void main(String[] args) {

		PropertyConfigurator.configure("log4j.properties");

		// Look and feel

//		try {
//			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
//		} catch (ClassNotFoundException e) {
//			logger.debug("Error on loading window look and feel : " + e.getMessage());
//		} catch (InstantiationException e) {
//			logger.debug("Error on loading window look and feel : " + e.getMessage());
//		} catch (IllegalAccessException e) {
//			logger.debug("Error on loading window look and feel : " + e.getMessage());
//		} catch (UnsupportedLookAndFeelException e) {
//			logger.debug("Error on loading window look and feel : " + e.getMessage());
//		}
//
//		final ImportForm imf = new ImportForm();

		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
//				imf.createGUISteps();
				createMainGUI();
			}
		});

	}

}
